<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['statuss']=array(
	'0'=>'禁用',
	'1'=>'启用',
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$return['params']=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$fields=array('id','title','createtime','plugin','status');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_diy_menu',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	if($list){
		foreach($list as &$row){
			$row['createtime']=date('20y/m/d H:i:s',$row['createtime']);
			if($row['plugin']=='core'){
				$row['plugin']='所有插件';
			}else{
				$row['plugin']=pdo_getcolumn('core_plugins',array('name'=>$row['plugin']),'title');
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'底部菜单列表'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]:这里是站点全局底部菜单，对应很多站点页面，插件页面，如果没有底部菜单的时候，就会显示本地开启的菜单！','list'=>array(
			'0'=>array('name'=>'title','title'=>'菜单名称','type'=>'string'),
			'1'=>array('name'=>'plugin','title'=>'生效范围','type'=>'string'),
			'2'=>array('name'=>'status','title'=>'菜单状态','type'=>'switch','set'=>'set'),
			'3'=>array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_diy_menu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['data']=json_decode($item['data'],true);
	}else{
		$item=array('data'=>array('menus'=>array()));
	}
	$return['item']=$item;
	$plugins=pdo_getall('core_plugins',array('type <'=>4),array('name','title'),'name');
	$plugins[]=array('name'=>'core','title'=>'所有插件');
	$return['plugins']=$plugins;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'页面',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'底部菜单列表'),
			'1'=>array('op'=>'edit','title'=>'底部菜单编辑'),
		),
		'edits'=>array(
			array(
				'title'=>'基础信息',
				'list'=>array(
					array('type'=>'select','title'=>'生效范围','name'=>'plugin', 'option_value'=>'name','options'=>'plugins','help'=>'设置该菜单生效范围，如果选择所有插件，则对所有插件生效。注意，如果插件自己设置了单独的底部菜单，则插件自己的底部菜单生效' ),
					array('type'=>'string','title'=>'菜单名称','name'=>'title', 'help'=>'' ),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','options'=>'statuss' ),
					array('type'=>'color','title'=>'图标颜色','tovalue'=>'pagedata.item.data','name'=>'iconcolor','help'=>'默认统一按站点设置里面的主题配色' ),
					array('type'=>'color','title'=>'文字颜色','tovalue'=>'pagedata.item.data','name'=>'textcolor','help'=>'' ),
				)
			),
			array(
				'title'=>'管理菜单，菜单数量建议在5个以内。菜单从左网友排列，排序值越大越靠右。具体请查看手机端效果',
				'list'=>array(
						array(
							'type'=>'array',
							'name'=>'menus',
							'add_tip'=>'添加一个菜单',
							'tovalue'=>'pagedata.item.data',
							'list'=>array(
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'' ),
								array('type'=>'icon','title'=>'图标','name'=>'icon','from'=>'jinyunicon','help'=>'' ),
								array('type'=>'string','title'=>'文字','name'=>'title','help'=>'' ),
								array('type'=>'string','title'=>'链接','name'=>'url','help'=>'' ),
							),'details'=>array(
								array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>''),
								array('type'=>'icon','name'=>'icon','from'=>'jinyunicon','title'=>'图标','help'=>''),
								array('type'=>'string','name'=>'title','title'=>'文字','help'=>''),
								array('type'=>'link','name'=>'url','from'=>'action','title'=>'链接','help'=>''),
							)
						)
				)
			)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['data']['menus']){
		usort($postdata['data']['menus'],'menu_paixu');
	}
	$data=array(
		'plugin'=>trim($postdata['plugin']),
		'title'=>trim($postdata['title']),
		'data'=>ijson_encode($postdata['data']),
		'status'=>intval($postdata['status'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_diy_menu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_diy_menu',$data);
	}
	exi('操作成功！');
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$status=intval($post['status']);
	pdo_update('core_diy_menu',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_diy_menu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			if(intval($id)){
				pdo_delete('core_diy_menu',array('id'=>intval($id),'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
function menu_paixu($a,$b){
	$pa=intval($a['paixu']);
	$pb=intval($b['paixu']);
	if($pa<=$pb){
		return -1;
	}else{
		return 1;
	}
}